<template>
  <div class="login_container">
    <el-row>
      <!-- :xs="0"代表小于768一分都不占 -->
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password"> 
            <el-input
              show-password
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="default"
              class="login_btn"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter,useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import {getTime} from '@/utils/time'
let useStore = useUserStore()
let loginForm = reactive({ username: 'admin', password: 'atguigu123' })
//控制按钮加载效果
let loading = ref(false)
let $router = useRouter()
let loginForms = ref()
let $route = useRoute()
const login = async () => {
  //保证全部表单符合校验才发请求
  await loginForms.value.validate()
  loading.value = true
  // 通知仓库发送登录请求
  // 请求成功-》首页展示数据的地方
  // 请求失败-》弹出登陆失败的信息
  try {
    await useStore.userLogin(loginForm)
    // 判断登陆的时候，路由路径当中有没有query参数 有就往query参数跳，没有就跳转到首页
    let redirect:any = $route.query.redirect
    $router.push({path:redirect || '/'})
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title:`Hi,${getTime()}好`
    })
    loading.value = false
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
// value表单的文本内容 callback 符合就放行 不符合就错误提示
const validatorUserName=(rule:any,value:any,callback:any )=>{
  if(value.length >= 5){
    callback()
  }else{
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPassword = (rule:any,value:any,callback:any)=>{
  if(value.length >= 6){
    callback()
  }else{
    callback(new Error('密码长度至少五位'))
  }
}
const rules = {
  username:[
    // {required:true,min:6,max:10,message:'账号长度至少六位',trigger:'change'}
    {trigger:'change',validator:validatorUserName,}
  ],
  password:[
    // {required:true,min:6,max:15,message:'密码长度至少6位',trigger:'change'}
    {trigger:'change',validator:validatorPassword,}
  ]
}
</script>
<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    width: 80%;
    top: 30dvh;
    position: relative;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;
    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
